/**
* File Name: session_fork.c
* Auther: Jichang
* Environment: Fedora 7 server
* To fork and test session function.
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

int main(int argc, char *argv[])
{
	pid_t pid;
	if((pid = fork()) < 0) {
		perror("fork error");
		exit(1);
	} else if(pid == 0) {
		printf("child process PID: %d\n", getpid());
		printf("Group ID of child: %d\n", getpgid(0));
		printf("Session ID of child: %d\n", getsid(0));

		// child-proc is not group-leader, 
		// it will be new session first-proc 
		// and be group-leader-proc.
		sleep(10);
		setsid();

		printf("Changed:\n");
		printf("child process PID: %d\n", getpid());
		printf("Group ID of child: %d\n", getpgid(0));
		printf("Session ID of child: %d\n", getsid(0));

		sleep(20);
		exit(0);
	}
	
	return 0;
}

